//
//  SearchViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/7/12.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class SearchViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UITextFieldDelegate
{
    var modelList: [keywordQuery]?{
        didSet{
            self.collection.reloadData()
        }
    }
    var type: String = ""
    
    @IBOutlet weak var actionView_x: NSLayoutConstraint!
    @IBOutlet weak var collection: UICollectionView!
    @IBOutlet weak var searchView: UITextField!
    @IBOutlet weak var actionView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.searchView.returnKeyType = UIReturnKeyType.Search
        self.collection.delegate = self
        self.collection.dataSource = self
        self.searchView.delegate = self
        let cellNib = UINib(nibName: "SearchCollCell", bundle: nil)
        self.collection.registerNib(cellNib, forCellWithReuseIdentifier: "SearchCollCell")
       // self.collection.scrollEnabled = false
        self.networkingKeywordQuery("cp")
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.hidden = true
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    //按钮的点击事件
    @IBAction func productBtnClicked(sender: UIButton){
        UIView.animateWithDuration(0.3, animations: {() -> Void in
            self.actionView_x.constant = sender.frame.origin.x
            self.actionView.layoutIfNeeded()
        })
         self.type = "cp"
        self.networkingKeywordQuery("cp")
    }
    @IBAction func companyBtnClicked(sender: UIButton){
        UIView.animateWithDuration(0.3, animations: {() -> Void in
            self.actionView_x.constant = sender.frame.origin.x
            self.actionView.layoutIfNeeded()

        })
       self.type = "gs"
        self.networkingKeywordQuery("gs")
    }
    @IBAction func topicBtnClicked(sender: UIButton){
        UIView.animateWithDuration(0.3, animations: {() -> Void in
            self.actionView_x.constant = sender.frame.origin.x
            self.actionView.layoutIfNeeded()

        })
       self.type = "zt"
        self.networkingKeywordQuery("zt")
    }
    
    //搜索按钮的点击事件
    @IBAction func searchBtnClicked(sender: UIButton){
        
        if self.searchView.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入关键字")
           
            return
        }else{
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SerarchResultVC") as! SerarchResultVC
            switch self.type {
            case "cp":
                vc.type = "1"
            case "gs":
                vc.type = "2"
            case "zt":
                vc.type = "3"
            default : vc.type = "1"
            }
            vc.keyword = self.searchView.text!
            self.navigationController?.pushViewController(vc, animated: true)
        }
       
    }
    // MARK: - UITextField Delegate
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SerarchResultVC") as! SerarchResultVC
        switch self.type {
        case "cp":
            vc.type = "1"
        case "gs":
            vc.type = "2"
        case "zt":
            vc.type = "3"
        default : vc.type = "1"
        }
        
        vc.keyword = self.searchView.text!
        
        self.navigationController?.pushViewController(vc, animated: true)
        
        return true
    }
    //MARK:UICollectionViewDataSource
    //每一组有多少个cell
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection section: Int) -> Int{
        
        if self.modelList?.count == nil{
            return 0
        }else{
            return (self.modelList?.count)!
        }
    }
    //设置每组的cell的边界
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
    }
    //最小行间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //最小列间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //设定全局的Cell尺寸
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        return CGSizeMake(SCREEN_WIDTH/2-20,30)
        
    }
    //cell上显示的信息
    func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell{
        
        let cell = collection.dequeueReusableCellWithReuseIdentifier("SearchCollCell", forIndexPath: indexPath) as! SearchCollCell
        
        let model = self.modelList![indexPath.row]
        cell.nameLbl.text = model.body
        
        return cell
        
    }
    //MARK:UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SerarchResultVC") as! SerarchResultVC
        switch self.type {
        case "cp":
            vc.type = "1"
        case "gs":
            vc.type = "2"
        case "zt":
            vc.type = "3"
        default : vc.type = "1"
        }
        
        vc.keyword = String(self.modelList![indexPath.item].body)
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    //MARK:NETWOKING
    func networkingKeywordQuery(type: String){
        DataTool.keywordQueryInfo(type, completionHandler:{ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.modelList = newDataes
            
        })
    }
    
    
 
    
}
